clear
infile int ccode1 int ccode2 int year long xrcomp1 long xrcomp2 long xropen1 long xropen2 long xconst1 long xconst2 long parcomp1 long parcomp2  using "C:\Users\mmousseau\Documents\Working Papers Data Sets\Werner Pol Sim.out"

* The next command drops the case that was read 
*  if the user included a line of variable names

drop if missing(ccode1) | missing(year)

* Interpret missing values 
mvdecode ccode1 ccode2 year , mv(-9)
mvdecode xrcomp1, mv(-99)
mvdecode xrcomp2, mv(-99)
mvdecode xropen1, mv(-99)
mvdecode xropen2, mv(-99)
mvdecode xconst1, mv(-99)
mvdecode xconst2, mv(-99)
mvdecode parcomp1, mv(-99)
mvdecode parcomp2, mv(-99)

* Variable labels where available (see EUGeneDocumentation.doc for full information) 
label variable ccode1 "COW CCode Num for state 1"
label variable ccode2 "COW CCode Num for state 2"
label variable year "Year of observation"

compress
gen dyadid=(10000*ccode1)+ccode2
sort dyadid

foreach v in xrcomp1 xrcomp2 xropen1 xropen2 xconst1 xconst2 parcomp1 parcomp2  {
gen n`v'=`v'
mvdecode n`v', mv(-88)
mvdecode n`v', mv(-77)
mvdecode n`v', mv(-66)
by dyadid: ipolate n`v' year, gen(ip`v')
replace ip`v'=. if `v'==-77 | `v'==-66 | `v'==.
drop n`v'
}
gen PolDistrans=sqrt((((ipxrcomp1-ipxrcomp2)/3)^2)+(((ipxropen1-ipxropen2)/4)^2)+(((ipxconst1-ipxconst2)/6)^2)+(((ipparcomp1-ipparcomp2)/5)^2))
label var PolDistrans "We account only for -88"
*The imputations made in this variable are analogus to creation of Polity2, except we dont impute -77.


*Cases of �interregnum,� or anarchy, are converted to a �neutral�: xrcomp 1.5, nxropen 2, nxconst 4, nparcomp 2.5
foreach v in xrcomp1 xrcomp2 xropen1 xropen2 xconst1 xconst2 parcomp1 parcomp2  {
gen n`v'=`v'
mvdecode n`v', mv(-88)
mvdecode n`v', mv(-66)
}
replace nxrcomp1=1.5 if nxrcomp1==-77
replace nxrcomp2=1.5 if nxrcomp2==-77
replace nxropen1=2 if nxropen1==-77
replace nxropen2=2 if nxropen2==-77
replace nxconst1=4 if nxconst1==-77
replace nxconst2=4 if nxconst2==-77
replace nparcomp1=2.5 if nparcomp1==-77
replace nparcomp2=2.5 if nparcomp2==-77
foreach v in xrcomp1 xrcomp2 xropen1 xropen2 xconst1 xconst2 parcomp1 parcomp2  {


by dyadid: ipolate n`v' year, gen(full`v')
replace full`v'=. if `v'==-66 | `v'==.
drop n`v'
}
gen PolDisfull=sqrt((((fullxrcomp1-fullxrcomp2)/3)^2)+(((fullxropen1-fullxropen2)/4)^2)+(((fullxconst1-fullxconst2)/6)^2)+(((fullparcomp1-fullparcomp2)/5)^2))
label var PolDisfull "We account for -88 & -77"
*The imputations made in this variable are analogus to creation of Polity2.
 foreach v in xrcomp1 xrcomp2 xropen1 xropen2 xconst1 xconst2 parcomp1 parcomp2  {
 mvdecode `v', mv(-88)
 mvdecode `v', mv(-77)
 mvdecode `v', mv(-66)
 }

gen PolDis=sqrt((((xrcomp1-xrcomp2)/3)^2)+(((xropen1-xropen2)/4)^2)+(((xconst1-xconst2)/6)^2)+(((parcomp1-parcomp2)/5)^2))
label var PolDis "Political Dissimilarity"
*No imputations made in the creation of this variable.

foreach v in xrcomp1 xrcomp2 xropen1 xropen2 xconst1 xconst2 parcomp1 parcomp2  {
drop `v' full`v' ip`v'
}
save "C:\Users\mmousseau\Documents\Working Papers Data Sets\Political Dissimilarity Werner.dta", replace
